// 大数据第六题
// author: lbc
package homework1;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class touch_delete {
    public static void main(String[] args) throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        String hdfsPath = "hdfs://localhost:9000";
        FileSystem hdfs = FileSystem.get(new URI(hdfsPath), conf);
        String filePath = "hdfstest/input.txt";
        String dirPath = "hdfstest";
        if (fileHadoopExist(hdfs, filePath)) {
            remove(hdfs, filePath);
            System.out.println("Delete Path: " + filePath);
        } else {
            if (!fileHadoopExist(hdfs, dirPath)) {
                makedir(hdfs, dirPath);
                System.out.println("Make dir: " + dirPath);
            }
            touchfile(hdfs, filePath);
            System.out.println("Touch File: " + filePath);
        }
        hdfs.close();
    }

    public static boolean makedir(FileSystem fs, String dirPath) throws IOException {
        boolean res = fs.mkdirs(new Path(dirPath));
        return res;
    }

    public static boolean remove(FileSystem fs, String file_path) throws IOException {
        boolean res = fs.delete(new Path(file_path), false);
        return res;
    }

    public static void touchfile(FileSystem fs, String file_path) throws IOException {
        FSDataOutputStream out = fs.create(new Path(file_path));
        out.close();
    }

    public static boolean fileHadoopExist(FileSystem fs, String file_path) throws IOException {
        return fs.exists(new Path(file_path));
    }

}
